package com.lut.edu.apm.commodityCenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lut.edu.apm.commodityCenter.dao.CommodityOrderMapper;
import com.lut.edu.apm.commodityCenter.pojo.CommodityOrder;
import com.lut.edu.apm.commodityCenter.service.CommodityOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;


/**
 * @author Zhao Suzhen
 * @date `2024/4/13`
 */
@Service
public class CommodityOrderServiceImpl extends ServiceImpl<CommodityOrderMapper, CommodityOrder> implements CommodityOrderService {
    @Autowired
    CommodityOrderMapper commodityOrderMapper;

    @Override
    public CommodityOrder queryById(String id) {
        CommodityOrder commodityOrder = commodityOrderMapper.selectById(id);
        if ("1".equals(commodityOrder.getStatus())) {
            return commodityOrder;
        }
        return null;
    }

    @Override
    public boolean updateReserve(String id, int reserve, String nowTime) {
        return commodityOrderMapper.updateReserve(id, reserve, nowTime) > 0;
    }

    @Override
    public boolean updateReservePay(String id, int reserve) {
        return commodityOrderMapper.updateReservePay(id, reserve) > 0;
    }

    @Override
    public Page<CommodityOrder> queryProductPaging(int startPaging, String productName, String
            brand, String company, String queryProductItem, List<Integer> reserve) {
        LambdaQueryWrapper<CommodityOrder> wrapper = Wrappers.lambdaQuery();
        if (!"".equals(productName)) {
            wrapper.like(CommodityOrder::getProductName, productName);
        }
        if (!"".equals(brand)) {
            wrapper.like(CommodityOrder::getBrand, brand);
        }
        if (!"".equals(company)) {
            wrapper.like(CommodityOrder::getCompany, company);
        }
        if (!"".equals(queryProductItem)) {
            wrapper.like(CommodityOrder::getProductItem, queryProductItem);
        }
        if (null != reserve && !reserve.isEmpty() && reserve.size() >= 2) {
            // 原符号       <       <=      >       >=      <>
            // 对应函数    lt()     le()    gt()    ge()    ne()
            wrapper.ge(CommodityOrder::getReserve, reserve.get(0));
            wrapper.le(CommodityOrder::getReserve, reserve.get(1));
        }
        wrapper.eq(CommodityOrder::getStatus, "1");
        Page<CommodityOrder> page = new Page<>(startPaging, 5);
        return commodityOrderMapper.selectPage(page, wrapper);
    }

    @Override
    public List<Map<String, Object>> queryFuzzyName(String queryName, String queryValue) {
        LambdaQueryWrapper<CommodityOrder> wrapper = Wrappers.lambdaQuery();
        if ("productName".equals(queryName)) {
            wrapper.like(StringUtils.hasText(queryValue), CommodityOrder::getProductName, queryValue);
        } else if ("productBrand".equals(queryName)) {
            wrapper.like(StringUtils.hasText(queryValue), CommodityOrder::getBrand, queryValue);
        } else if ("productCompany".equals(queryName)) {
            wrapper.like(StringUtils.hasText(queryValue), CommodityOrder::getCompany, queryValue);
        } else if ("productProductItem".equals(queryName)) {
            wrapper.like(StringUtils.hasText(queryValue), CommodityOrder::getProductItem, queryValue);
        }
        wrapper.eq(CommodityOrder::getStatus, "1");
        List<CommodityOrder> commodityOrderList = commodityOrderMapper.selectList(wrapper);
        List<Map<String, Object>> result = new ArrayList<>();
        for (CommodityOrder commodityOrder : commodityOrderList) {
            HashMap<String, Object> item = new HashMap<>();
            if ("productName".equals(queryName)) {
                item.put("value", commodityOrder.getProductName());
            } else if ("productBrand".equals(queryName)) {
                item.put("value", commodityOrder.getBrand());
            } else if ("productCompany".equals(queryName)) {
                item.put("value", commodityOrder.getCompany());
            } else if ("productProductItem".equals(queryName)) {
                item.put("value", commodityOrder.getProductItem());
            }
            result.add(item);
        }

        return new ArrayList<>(new HashSet<>(result));
    }

    @Override
    public int queryMaxReserve() {
        QueryWrapper<CommodityOrder> wrapper = new QueryWrapper<>();
        wrapper.select("max(reserve) as max");
        List<Map<String, Object>> maps = commodityOrderMapper.selectMaps(wrapper);
        int count = 0;
        if (null != maps) {
            count = (int) maps.get(0).get("max");
        }
        return count;
    }

    @Override
    public void updateProduct(CommodityOrder commodityOrder) {
        commodityOrderMapper.update(commodityOrder, new UpdateWrapper<CommodityOrder>().eq("barcode", commodityOrder.getBarcode()));
    }

    @Override
    public List<Map<String, Object>> queryNewProduct() {
        return commodityOrderMapper.selectNewProduct();
    }


    @Override
    public long queryProductPrice(String productName) {
        LambdaQueryWrapper<CommodityOrder> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CommodityOrder::getProductName, productName);
        wrapper.eq(CommodityOrder::getStatus, "1");
        return commodityOrderMapper.selectOne(wrapper).getPriceA();
    }
}
